<?php
include_once 'app/until/GetData.php';
include_once 'app/until/GFW_HTTP.php';

class Mail extends GetData{

	private $daten     = null;
	public $mails      = null;
	public $ActivUser  = null;	
	public $System     = null;
	public $error      = false;

	public function __construct($match){
		$this->daten = $match;
	}

	public function getWort(RainTPL $design){	    
	    $this->design = $design;	    
        parent::GetData();	
        $user_page_static = new Db_user_page_static();
        $DBMail = new Db_Mail();
		$crumb = new Crumb($design,2);
		
		$navi = $user_page_static->getNavi("Pages","Deine Seiten");		
        $this->design->assign ('navi',$navi);
		        
		$this->design->assign ('css', "mail");
        $this->design->assign ('js', "jsHome");
		$this->design->assign ('bcn', $crumb->getCrumb_Name());
		$this->design->assign ('bcrumb',$crumb->getCrumb());
           
		if ($this->System->post->gfw_senden){
		        
		        $for = $this->GetUserByName($this->System->post->gfw_to);

	            if(!$DBMail->SendeMail($this->ActivUser->id,$for,$this->System->post->gfw_subject,$this->System->post->gfw_text,0))
				{
				    $this->error .= "Mail konnte nicht an $for gesendet werden <br />";
				}
		        HTTP::redirect ( BASE_URL . '/mailbox?succes='.$for);				
		}
		elseif(isset($this->daten['Mail_ID']))
		{
			$mail = $DBMail->GetMail($this->ActivUser->id,$this->daten['Mail_ID']);
				
			if($mail){
			    if($mail->answer_id){
			        $answer = $DBMail->GetLastMail($this->ActivUser->id,$mail->answer_id);
                    if($answer){
                        $answer->text = GFW_Validate::ConvertBBcode($answer->text);
			            $this->design->assign ('old_mail',$answer);
                    }
                }
				$DBMail->ChangeOnReaded($this->daten['Mail_ID']);
		        $mail->text = GFW_Validate::ConvertBBcode($mail->text);
				$this->design->assign ('mail',$mail);
				$this->System->design->display($this->design->ln.'/mail/read.tpl');
			}
			else
			{
				HTTP::redirect ( BASE_URL . '/mail/mailbox');
			}
		}
		elseif(isset($this->daten['Answer_ID']))
		{
			$mail = $DBMail->GetMail($this->ActivUser->id,$this->daten['Answer_ID']);
				
			if($mail){
			    $check_subject = array();
			    preg_match_all("/Re-(\d+):(.+)/ie", $mail->subject,$check_subject);
			    		    
				if(isset($check_subject[1][0])){
			        $this->design->assign ('subject','Re-'.($check_subject[1][0]+1).':'.$check_subject[2][0]);
				}else{
				    $this->design->assign ('subject','Re-1:'.$mail->subject);
				}
				$mail->text = GFW_Validate::ConvertBBcode($mail->text);
			    $this->design->assign ('mail',$mail);
				$this->System->design->display($this->design->ln.'/mail/answer.tpl');
			}
			else
			{
				HTTP::redirect ( BASE_URL . '/mail/mailbox');
			}
		}
		else
		{		    
			$this->System->design->display($this->design->ln.'/mail/new.tpl');
		}
			
	}

}
?>